<?php

use Doraemon\service\Point;
use Doraemon\cache\HotValue;
use Doraemon\model\TopicActivity;
use Doraemon\cache\TopicCounter;
use Biz\model\activity\SuperBuyPlanet;
use Doraemon\amqp\CommunityQueue;
use Doraemon\tools\BlockType;
use Doraemon\tools\PointConfig as tPointConfig;
use Biz\cache\activity\SuperBuyerPlanetMess;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\pockets\constInterface\statSign\AutoModule;
use hhz\constant\redis\redisKeys;



class HeadInfoAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REALNAME;

    const TAB_NAME = [
         '收纳',
         '软装',
         '硬装',
         '清洁',
         '厨电',
         '生活'
    ];

    const WIKI_HOT_TOPIC = 5;

    /**
     * @throws Exception
     */
    public function main()
    {
        $uid = $this->uid;
        $data = [];
        $info =  SuperBuyPlanet::getByUid($uid);
        if(empty($info)){
            $data['progress_type'] = 0 ;
            SuperBuyPlanet::inserData([
                'uid' => $uid ,
                'fav_wiki_cnt' => 0
            ]);
            //发送见面礼积分
            CommunityQueue::insertQueue(tPointConfig::MQ_QUEUE_URL, [
                'event_name' => tPointConfig::SUPER_BUY_STAR_FIRST_MEET,
                'obj_id' => 0,//某次记录的id
                'uid' => $this->uid,
                'point' => 5,
                'type' => tPointConfig::POINT_AWARD
            ], CommunityQueue::QUEUE_COMMUNITY);
            //发送见面礼积分
            $data['toast_mess'] = '星球见面礼+5';//toast message
            $data['pop_card'] = false;//弹窗卡片
            $data['user_point'] = Point::getAmount($uid) + 5;
        }else{
            $data['progress_type'] = (int)$info['fav_wiki_cnt'];
            $toast_mess =  SuperBuyerPlanetMess::getItem($this->uid);
//            $redis = new cacheRedis(redisKeys::KEY_C_CACHE_SUPER_BUYER_PLANET_MESS);
//            $redis->getField($this->uid);
            $data['pop_card'] = false;//弹窗卡片
            $data['toast_mess'] = "";

            if( $toast_mess < 5 && $data['progress_type']  >  $toast_mess ){
                if($data['progress_type'] >= 5){
                    $data['pop_card'] = true;
                }else{
                    $data['toast_mess'] = "挖宝成功";
                }
                SuperBuyerPlanetMess::setItems($this->uid , $data['progress_type'] );
//                $redis->setCache($this->uid, $data['progress_type']);
            }

            $data['user_point'] = Point::getAmount($uid);
        }

//        $redisTime = new cacheRedis(redisKeys::SUPER_BUYER_PLANET_TIME);
        if( time() >= SuperBuyerPlanetMess::getTimeItem("start_time")  && time() <= SuperBuyerPlanetMess::getTimeItem("end_time")){
//            if( time() >= $redisTime->getField("start_time")  && time() <= $redisTime->getField("end_time")){
            $data['end_time'] =  (int)SuperBuyerPlanetMess::getTimeItem("end_time") ? : strtotime("+1 day") ;//持续时间
        }
        $data['nice_goods_classify_tab'] =self::TAB_NAME ;
        $data['hot_topic']= self::getHotTopicList(); //热门话题

        $data['statSign'] = basicStatSign::genStatSign(AutoModule::MODULE_COMMON_ACTIVITY_SHOPPING_MALL, [
            basicStatSign::EXTEND_KEY_OBJ_TYPE => 1104,
        ], [ 'obj_type' => 1104, 'activity_name' => "buyers_land"]);

        $this->setSuccess($data);
    }

    public static function getHotTopicList()
    {

        $hotTopics = [];
        $scoreList =  HotValue::getRankList(self::WIKI_HOT_TOPIC, 0, 3, 1);
        if($scoreList && is_array($scoreList)){
            $topicIds = array_keys($scoreList);
            $topics = TopicActivity::getByIds($topicIds);
            $wholeCounter = TopicCounter::getMulti($topicIds);
            if(empty($topics)) return [];
            $topicInfo = array_combine(array_column($topics , 'id') , $topics);
            $avatars =    \Biz\module\Classes\wiki\Wiki::getRandUserCoverImg(12);
            $avatars = array_column($avatars , "cover_img");
            foreach ($topicIds as $key => $topicId) {
                $topicAvatars = [];
                $topicAvatars[]  = array_pop($avatars);
                $topicAvatars[]  = array_pop($avatars);
                $topicAvatars[]  = array_pop($avatars);
                $hotTopics[$key]['topic_id'] = $topicId;
                $hotTopics[$key]['title']   = $topicInfo[$topicId]['title'];
                $hotTopics[$key]['answer']  = $wholeCounter[$topicId]['answer'];
                $hotTopics[$key]['joiner']  = $wholeCounter[$topicId]['joiner'];
                $hotTopics[$key]['avatars'] = $topicAvatars;
                $hotTopics[$key]['statSign'] = basicStatSign::genStatSign(AutoModule::MODULE_COMMON_ACTIVITY_TOPIC_CARD, [
                    basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_TOPIC_ACTIVITY,
                    basicStatSign::EXTEND_KEY_OBJ_ID => $topicId,
                ], [ "obj_id" => $topicId, 'obj_type' => 63, 'activity_name' => "buyers_land"]);
            }
        }
        return $hotTopics;


    }





}